// pages/topicdet/index.js
var app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    show: false,
    serverUrl: '',
    topicobj:{},
    userInfo:{},
    items:[],
    iamges:[],
    answerinfo:[],
    inputValue:{},
    aliyunOss: app.data.aliyunOss,
    server:app.data.server,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _this=this;
    app.paiRequest("/app/topic/conversation/detail", options).then(function (res) {
      console.log(res);
      var data=res.data;
      var topicobj=data[0];
      var items = topicobj.itemize;
      var imagePath = topicobj.topicImgPath;
      var arry = items.split(",");
      var imgs = imagePath.split(",")
      _this.setData({
        topicobj: topicobj,
        items: arry,
        iamges: imgs,
        serverUrl: app.data.server,
        userInfo: app.globalData.userInfo
      })
    })
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  navigateTo: function (e) {
    console.log(e)
    this.setData({
      show:true
    })
  },
  inputValue:function(e){
    this.setData({
      inputValue:e.detail.value
    })
  },
  sendInfo:function(){
    var _this=this;
    var inputValue = this.data.inputValue;
    var commentObj={};    
    var userInfo = this.data.userInfo;
    var openid = app.data.openid;
    var topicId = this.data.topicobj.id;
    userInfo.commentContent=inputValue;
    commentObj.commentContent = inputValue;
    userInfo.topicId = topicId;
    commentObj.topicId = topicId;
    userInfo.commentator = userInfo.id;
    commentObj.commentator = userInfo.id;
    commentObj.nickName = userInfo.userName;
    commentObj.avatarUrl = userInfo.avatar;
    this.setData({      
      show:false
    })
    app.paiRequest("/app/topic/comment/add", commentObj).then(function (res) {
      var data=res.data;
      data.likeNum=0;
      var topicobj=_this.data.topicobj
      var commentList = topicobj.commentList;
      commentList.push(data);
      topicobj.commentList = commentList;
      _this.setData({
        topicobj: topicobj
      })
    })
  },
  addLikeNum:function(e){
     var _this=this;
     var index=e.currentTarget.dataset.index;
    var id = e.currentTarget.dataset.id;
    var topicobj = this.data.topicobj;
    var commentList = topicobj.commentList;
    var commentObj = commentList[index];
    commentObj.likeNum = commentObj.likeNum+1;
    commentObj.likeYesNo=1;
    commentList[index] = commentObj;
    topicobj.commentList = commentList;
    _this.setData({
      topicobj: topicobj
    })
    app.paiRequest("/app/topic/comment/addLikeNum", { id: id, likeNum: commentObj.likeNum, commentator: _this.data.userInfo.id}).then(function (res) {
       console.log(res)
    })
  }
})